/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.api;

import com.arisgo.cloud.auth.sys.service.UserRoleService;
import com.arisgo.cloud.auth.sys.vo.RoleVo;
import com.arisgo.cloud.auth.sys.vo.UserVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Search;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户角色 Api
 *
 * @author Arisgo
 * @since 2024-08-03
 */
@RestController
@AllArgsConstructor
@Tag(name = "用户角色 API", description = "用户角色CRUD操作")
public class UserRoleApi {

    private final UserRoleService userRoleService;


    /**
     * 分页查询关联关系表（角色）
     */
    @PostMapping(value = "/user/{userId}/role/page")
    @Operation(summary = "分页查询关联关系表（角色）")
    public Result<Pagination<RoleVo>> pageUserRole(@PathVariable(value = "userId") Long userId,
                                                   @RequestBody Paging paging) {
        return Result.success(userRoleService.pageUserRole(userId, paging));
    }

    /**
     * 条件查询关联关系表（角色）
     */
    @GetMapping(value = "/user/{userId}/role/find")
    @Operation(summary = "条件查询关联关系表（角色）")
    public Result<List<RoleVo>> findUserRole(@PathVariable(value = "userId") Long userId,
                                             @RequestParam(value = "tenantId") Long tenantId) {
        return Result.success(userRoleService.findUserRole(userId, tenantId));
    }

    /**
     * 保存关联数据（角色）
     */
    @PostMapping(value = "/user/{userId}/role")
    @Operation(summary = "保存关联数据（角色）")
    public Result<Boolean> saveUserRole(@PathVariable(value = "userId") Long userId,
                                        @RequestBody List<Long> roleIds) {
        return Result.success(userRoleService.saveUserRole(userId, roleIds));
    }

    /**
     * 删除关联数据（角色）
     */
    @DeleteMapping(value = "/user/{userId}/role")
    @Operation(summary = "删除关联数据（角色）")
    public Result<Boolean> deleteUserRole(@PathVariable(value = "userId") Long userId,
                                          @RequestBody List<Long> roleIds) {
        return Result.success(userRoleService.removeUserRole(userId, roleIds));
    }

    /**
     * 分页查询关联关系表（用户）
     */
    @PostMapping(value = "/role/{roleId}/user/page")
    @Operation(summary = "分页查询关联关系表（用户）")
    public Result<Pagination<UserVo>> pageRoleUser(@PathVariable(value = "roleId") Long roleId,
                                                   @RequestBody Paging paging) {
        return Result.success(userRoleService.pageRoleUser(roleId, paging));
    }

    /**
     * 条件查询关联关系表（用户）
     */
    @PostMapping(value = "/role/{roleId}/user/find")
    @Operation(summary = "条件查询关联关系表（用户）")
    public Result<List<UserVo>> findRoleUser(@PathVariable(value = "roleId") Long roleId,
                                             @RequestBody Search search) {
        return Result.success(userRoleService.findRoleUser(roleId, search));
    }

    /**
     * 保存关联数据（用户）
     */
    @PostMapping(value = "/role/{roleId}/user")
    @Operation(summary = "保存关联数据（用户）")
    public Result<Boolean> saveRoleUser(@PathVariable(value = "roleId") Long roleId,
                                        @RequestBody List<Long> userIds) {
        return Result.success(userRoleService.saveRoleUser(roleId, userIds));
    }

    /**
     * 删除关联数据（用户）
     */
    @DeleteMapping(value = "/role/{roleId}/user")
    @Operation(summary = "删除关联数据（用户）")
    public Result<Boolean> deleteRoleUser(@PathVariable(value = "roleId") Long roleId,
                                          @RequestBody List<Long> userIds) {
        return Result.success(userRoleService.removeRoleUser(roleId, userIds));
    }

}
